package com.cee.admin.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.common.core.query.BasePageQuery;
import com.cee.common.utils.DateUtils;
import com.cee.common.utils.StringUtils;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.sys.entity.SysFrontUserLoginLog;
import com.cee.domain.modules.sys.vo.SysLoginLogExportVo;
import com.cee.ds.common.constants.PermissionConstants;
import com.cee.ds.modules.sys.service.ISysFrontUserLoginLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cee.admin.common.BaseController;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 前台用户登录日志表 前端控制器
 * </p>
 *
 * @author wsz
 * @since 2024-02-06
 */
@RestController
@RequestMapping("/api/sys/frontUser/loginLog")
@Tag(name = "前台用户-登录日志")
public class SysFrontUserLoginLogController extends BaseController {

    @Autowired
    private ISysFrontUserLoginLogService frontUserLoginLogService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @RequiresPermissions(PermissionConstants.SYS_FRONT_LOG_PREFIX + PermissionConstants.READ)
    public Result<IPage<SysFrontUserLoginLog>> page(BasePageQuery<SysFrontUserLoginLog> pageQuery,
                                                    String loginName,
                                                    Date startTime,
                                                    Date endTime,
                                                    @Valid @Size(max = 1) String status) {

        Page<SysFrontUserLoginLog> page = frontUserLoginLogService.page(pageQuery.generatePage(),
                frontUserLoginLogService.lambdaQueryWrapper()
                        .ge(startTime != null, SysFrontUserLoginLog::getStartTime, startTime)
                        .le(endTime != null, SysFrontUserLoginLog::getStartTime, endTime == null ? null : DateUtils.endOfDay(endTime))
                        .eq(StringUtils.isNotBlank(status), SysFrontUserLoginLog::getStatus, status)
                        .like(StringUtils.isNotBlank(loginName), SysFrontUserLoginLog::getLoginName, loginName)
                        .orderByDesc(SysFrontUserLoginLog::getStartTime));
        return success(page);
    }

    @Operation(summary = "导出")
    @PostMapping("export")
    @RequiresPermissions(PermissionConstants.SYS_FRONT_LOG_PREFIX + PermissionConstants.EXPORT)
    public void export(@RequestBody SysLoginLogExportVo exportVo) throws IOException {
        exportVo.setEndTime(exportVo.getEndTime() == null ? null : DateUtils.endOfDay(exportVo.getEndTime()));
        frontUserLoginLogService.export(wrapDownloadResponse("前台拥护登陆日志.xlsx"), exportVo);
    }
}
